package com.gxa.common.exception;

import com.gxa.common.vo.ResultVo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: wangyifan
 * @Date: 2021/12/30 9:33
 * @Description: $end$
 * @Version: V1.0
 */
@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public ResultVo returnArithmeticException(){
        ResultVo r = new ResultVo();
        r.setCode(500);
        r.setMsg("用户名错误");
        return r;
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public ResultVo returnIncorrectCredentialsException(){
        ResultVo r = new ResultVo();
        r.setCode(500);
        r.setMsg("用户密码错误");
        return r;
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public ResultVo returnUnauthorizedException(){
        ResultVo r = new ResultVo();
        r.setCode(500);
        r.setMsg("角色错误");
        return r;
    }

    @ExceptionHandler(LockedAccountException.class)
    @ResponseBody
    public ResultVo returnLockedAccountException(){
        ResultVo r = new ResultVo();
        r.setCode(500);
        r.setMsg("账号被锁");
        return r;
    }

    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public ResultVo returnAuthenticationException(){
        ResultVo r = new ResultVo();
        r.setCode(500);
        r.setMsg("认证错误");
        return r;
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public ResultVo returnAuthorizationException(){
        ResultVo r = new ResultVo();
        r.setCode(500);
        r.setMsg("授权异常");
        return r;
    }
}
